﻿using Protocol;
using Protocol.DTO;
using ServerFrame;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gameserver.Logic.Match
{
    public class MatchRoom:AbsBroadcastHandler
    {
        public int id;
        public int MaxNum;
        public string master_name;
        public List<Mytoken> teamone;
        public List<Mytoken> teamtwo;
        public List<string> teamonename;
        public List<string> teamtwoname;
        public MatchHandler handler;//匹配处理

        public MatchRoom(int id,string mname, MatchHandler handler, int MaxNum=5)
        {
            this.id = id;
            master_name = mname;
            this.MaxNum = MaxNum;
            this.handler = handler;
            Logic_Type = GameProtocol.match;
            Logic_Area = id;
            teamone = new List<Mytoken>();
            teamtwo = new List<Mytoken>();
            teamonename = new List<string>();
            teamtwoname = new List<string>();
        }
        public void Exit(Mytoken token ,string name)
        {
            int id;
            if(name.Equals(master_name))
            {
                foreach (Mytoken item in teamone)
                {
                    handler.RoomDic_token.TryRemove(item, out id);
                }
                foreach (Mytoken item in teamtwo)
                {
                    handler.RoomDic_token.TryRemove(item, out id);
                }
                //1 代表必须离开（房主离开）
                Broadcast(teamone,1, MatchProtocol.Exit_BRO);
                Broadcast(teamtwo,1, MatchProtocol.Exit_BRO);
                Clear();
                return;
            }
            if(teamone.Contains(token))
            {
                handler.RoomDic_token.TryRemove(token, out id);
                teamone.Remove(token);
                teamonename.Remove(name);
                //0 代表有人离开
                Broadcast(teamone, 0, MatchProtocol.Exit_BRO, GetRoom_DTO());
                Broadcast(teamtwo, 0, MatchProtocol.Exit_BRO, GetRoom_DTO());
                return;
            }
            if (teamtwo.Contains(token))
            {
                handler.RoomDic_token.TryRemove(token, out id);
                teamtwo.Remove(token);
                teamtwoname.Remove(name);
                Broadcast(teamone, 0, MatchProtocol.Exit_BRO, GetRoom_DTO());
                Broadcast(teamtwo, 0, MatchProtocol.Exit_BRO, GetRoom_DTO());
                return;
            }
        }
        public void Clear()
        {
            teamone.Clear();
            teamtwo.Clear();
            teamonename.Clear();
            teamtwoname.Clear();
        }
        public void Enter(Mytoken token ,string name)
        {
            if(teamone.Count>teamtwo.Count)
            {
                teamtwo.Add(token);
                teamtwoname.Add(name);
            }
            else
            {
                teamone.Add(token);
                teamonename.Add(name);
            }
            //0 代表进入成功 或 有人进入房间
            Broadcast(teamone, 0, MatchProtocol.Enter_BRO, GetRoom_DTO());
            Broadcast(teamtwo, 0, MatchProtocol.Enter_BRO, GetRoom_DTO());

        }
        public void Change(Mytoken token, string name)
        {
            int index = 0;
            foreach (string item in teamonename)
            {
                if(item.Equals(name))
                {
                    index = 1;
                    break;
                }
            }
            foreach (string item in teamtwoname)
            {
                if (item.Equals(name))
                {
                    index = 2;
                    break;
                }
            }
            if(index==1)
            {
                if(teamtwo.Count>=MaxNum)
                {
                    //2 队伍已满
                    Send(token, 2, MatchProtocol.Change_BRO);
                    return;
                }
                teamone.Remove(token);
                teamonename.Remove(name);
                teamtwo.Add(token);
                teamtwoname.Add(name);
                Broadcast(teamone, 0, MatchProtocol.Change_BRO, GetRoom_DTO());
                Broadcast(teamtwo, 0, MatchProtocol.Change_BRO, GetRoom_DTO());
            }
            else if(index==2)
            {
                if (teamone.Count >= MaxNum)
                {
                    //2 队伍已满
                    Send(token, 2, MatchProtocol.Change_BRO);
                    return;
                }
                teamtwo.Remove(token);
                teamtwoname.Remove(name);
                teamone.Add(token);
                teamonename.Add(name);
                Broadcast(teamone, 0, MatchProtocol.Change_BRO, GetRoom_DTO());
                Broadcast(teamtwo, 0, MatchProtocol.Change_BRO, GetRoom_DTO());
            }
        }
        public void Chat(string name,TransModel model)
        {
            string content = model.GetMessage<string>();
            content = name + ":" + content;
            Broadcast(teamone, 0, MatchProtocol.Chat_BRO, content);
            Broadcast(teamtwo, 0, MatchProtocol.Chat_BRO, content);
        }
        Room_DTO GetRoom_DTO()
        {
            Room_DTO dto = new Room_DTO(id, master_name);
            dto.MaxNum = MaxNum * 2;
            dto.num = teamone.Count + teamtwo.Count;
            dto.teamone = teamonename.ToArray<string>();
            dto.teamtwo = teamonename.ToArray<string>();
            return dto;
        }

    }
}
